package com.hundsun.documentripper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hundsun.documentripper.domain.dto.DemoSource;
import com.hundsun.documentripper.domain.dto.FileSource;
import com.hundsun.documentripper.domain.pojo.DemoNode;
import com.hundsun.documentripper.domain.pojo.File;
import com.hundsun.documentripper.domain.pojo.FileNode;
import com.hundsun.documentripper.domain.pojo.FileNodeHistory;
import com.hundsun.documentripper.mapper.FileNodeHistoryMapper;
import com.hundsun.documentripper.mapper.FileNodeMapper;
import com.hundsun.documentripper.service.FileNodeService;
import com.hundsun.documentripper.service.NodeContentService;
import com.hundsun.documentripper.service.NodeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：文件节点接口实现
 * @date ：Created in 2021/1/28
 */
@Service
public class FileNodeServiceImpl implements FileNodeService {

    @Autowired
    private FileNodeMapper fileNodeMapper;
    @Autowired
    private FileNodeHistoryMapper fileNodeHistoryMapper;
    @Autowired
    private NodeTemplateService nodeTemplateService;
    @Autowired
    private NodeContentService nodeContentService;

    @Override
    public List<FileNode> getFileNodeListById(String fileUuid) {
        QueryWrapper<FileNode> wrapper = new QueryWrapper<>();
        wrapper.eq("file_uuid", fileUuid);
        return fileNodeMapper.selectList(wrapper);
    }

    @Override
    public List<FileNodeHistory> getFileNodeHistoryListById(String fileUuid) {
        QueryWrapper<FileNodeHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("file_uuid", fileUuid);
        return fileNodeHistoryMapper.selectList(wrapper);
    }

    @Override
    public void deleteFileNodeById(String fileUuid) {
        QueryWrapper<FileNode> wrapper = new QueryWrapper<>();
        wrapper.eq("file_uuid", fileUuid);
        fileNodeMapper.delete(wrapper);
    }

    @Override
    public void deleteFileNodeHistoryById(String fileUuid) {
        QueryWrapper<FileNodeHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("file_uuid", fileUuid);
        fileNodeHistoryMapper.delete(wrapper);
    }

    @Override
    public void saveFileNodeList(File file, List<FileNode> fileNodeList, boolean isLast) {
        if (null != fileNodeList) {
            if (isLast) {
                for (FileNode fileNode : fileNodeList) {
                    fileNode.setFileUuid(file.getUuid());
                    fileNode.setCreateUser(file.getCreateUser());
                }
                fileNodeMapper.insertBatchSomeColumn(fileNodeList);
            } else {
                List<FileNodeHistory> fileNodeHistoryList = new ArrayList<>();
                for (FileNode fileNode : fileNodeList) {
                    fileNode.setFileUuid(file.getUuid());
                    fileNode.setCreateUser(file.getCreateUser());
                    fileNodeHistoryList.add(FileNodeHistory.fromFileNode(fileNode));
                }
                fileNodeHistoryMapper.insertBatchSomeColumn(fileNodeHistoryList);
            }
        }
    }

    @Override
    public void getNodeTemplateViewsAndNodeContents(FileSource fileSource, List<FileNode> fileNodeList) {
        // 获取依赖的节点模板UUID列表和内容UUID列表
        List<String> nodeTemplateUuidList = new ArrayList<>();
        List<String> nodeContentUuidList = new ArrayList<>();
        for (FileNode fileNode : fileNodeList) {
            if (!StringUtils.isEmpty(fileNode.getFileNodeTemplateUuid())) {
                nodeTemplateUuidList.add(fileNode.getFileNodeTemplateUuid());
            }
            if (!StringUtils.isEmpty(fileNode.getContentUuid())) {
                nodeContentUuidList.add(fileNode.getContentUuid());
            }
            if (!StringUtils.isEmpty(fileNode.getContentNodeTemplateUuid())) {
                nodeTemplateUuidList.add(fileNode.getContentNodeTemplateUuid());
            }
        }
        // 获取依赖的节点模板列表
        fileSource.setNodeTemplateViews(nodeTemplateService.getNodeTemplateView(nodeTemplateUuidList));
        // 获取依赖的内容列表
        fileSource.setNodeContents(nodeContentService.getNodeContentList(nodeContentUuidList));
    }

    @Override
    public void getNodeTemplateViewsAndNodeContents(DemoSource demoSource, List<DemoNode> demoNodeList) {
        // 获取依赖的节点模板UUID列表和内容UUID列表
        List<String> nodeTemplateUuidList = new ArrayList<>();
        List<String> nodeContentUuidList = new ArrayList<>();
        for (DemoNode demoNode : demoNodeList) {
            if (!StringUtils.isEmpty(demoNode.getDemoNodeTemplateUuid())) {
                nodeTemplateUuidList.add(demoNode.getDemoNodeTemplateUuid());
            }
            if (!StringUtils.isEmpty(demoNode.getContentUuid())) {
                nodeContentUuidList.add(demoNode.getContentUuid());
            }
            if (!StringUtils.isEmpty(demoNode.getContentNodeTemplateUuid())) {
                nodeTemplateUuidList.add(demoNode.getContentNodeTemplateUuid());
            }
        }
        // 获取依赖的节点模板列表
        demoSource.setNodeTemplateViews(nodeTemplateService.getNodeTemplateView(nodeTemplateUuidList));
        // 获取依赖的内容列表
        demoSource.setNodeContents(nodeContentService.getNodeContentList(nodeContentUuidList));
    }

}